package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.service.DishService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;



    /**
     * 添加菜品
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "dish ",allEntries = true)
    public R Save(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavors(dishDto);
        return R.success("添加菜品成功");
    }

    /**
     * 分页
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("page")
    public R<Page<DishDto>> page(Integer page, Integer pageSize, String name) {
        //传统的分页查询无法满足数据展示,因为要分类名称
        Page<DishDto> pageInfo = dishService.pageDishDto(page, pageSize, name);
        return R.success(pageInfo);

    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "dish ",allEntries = true)
    public R<String> deleteByIds(Long[] ids) {
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 根据id查询菜品详情信息(包含多种口味)
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<DishDto> getById(@PathVariable Long id) {

        DishDto dishDto = dishService.getDishByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 更新菜品数据以及口味信息
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "dish ",allEntries = true)
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);

        return R.success("修改成功");
    }

    /**
     * 启用禁用
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    @CacheEvict(cacheNames = "dish ",allEntries = true)
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //批量修改状态
        dishService.lambdaUpdate()
                .set(Dish::getStatus, status)
                .in(Dish::getId, ids)
                .update();

        return R.success("修改成功");
    }

    /**
     * 根据分类查菜品列表
     *
     * @param categoryId
     * @return
     */
    @GetMapping("list")
    @Cacheable( cacheNames = "dish",key = "#categoryId +'_'+#name")
    public R<List<DishDto>> listDto(Long categoryId, String name) {

        List<DishDto> dishDtoList = dishService.listDishDto(categoryId, name);

        return R.success(dishDtoList);
    }


}
